package com.fuyao.cloud.admin.message.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.message.dto.ManagementUserObjectSearchDTO;
import com.fuyao.cloud.message.vo.ManagementUserObjectVO;
import com.fuyao.cloud.message.dto.MessageManagementSaveDTO;
import com.fuyao.cloud.message.dto.MessageManagementUpdateDTO;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.common.security.annotation.Inner;
import com.fuyao.cloud.message.entity.MessageManagement;
import com.fuyao.cloud.admin.message.service.MessageManagementService;
import com.fuyao.cloud.message.vo.MessageRuleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息管理
 *
 * @author liyue
 * @date 2022-04-22 17:27:38
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/messagemanagement")
@Api(value = "messagemanagement", tags = "消息管理管理")
public class MessageManagementController {

    private final MessageManagementService messageManagementService;

    /**
     * 分页查询
     *
     * @param page              分页对象
     * @param messageManagement 消息管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('message_messagemanagement_view')")
    public R getMessageManagementPage(Page page, MessageManagement messageManagement) {
        return R.ok(messageManagementService.getMessageManagementPage(page, Wrappers.query(messageManagement)));
    }

    /**
     * 分页查询 (弹出窗口选择)
     *
     * @param page              分页对象
     * @param messageManagement 消息管理
     * @return
     */
    @ApiOperation(value = "分页查询(弹出消息分页选择)", notes = "分页查询(弹出消息分页选择)")
    @GetMapping("/dlgSelectPage")
    @PreAuthorize("@pms.hasPermission('message_messagemanagement_view')")
    public R page(Page page, MessageManagement messageManagement) {
        return R.ok(messageManagementService.page(page, Wrappers.query(messageManagement)));
    }

    /**
     * 通过id查询消息管理
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('message_messagemanagement_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(messageManagementService.getById(id));
    }

    /**
     * 新增消息管理
     *
     * @param messageManagement 消息管理
     * @return R
     */
    @ApiOperation(value = "新增消息管理", notes = "新增消息管理")
    @SysLog("新增消息管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('message_messagemanagement_add')")
    public R save(@RequestBody MessageManagementSaveDTO messageManagement) {
        return R.ok(messageManagementService.save(messageManagement));
    }

    /**
     * 修改消息管理
     *
     * @param messageManagement 消息管理
     * @return R
     */
    @ApiOperation(value = "修改消息管理", notes = "修改消息管理")
    @SysLog("修改消息管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('message_messagemanagement_edit')")
    public R updateById(@RequestBody MessageManagementUpdateDTO messageManagement) {
        return R.ok(messageManagementService.updateById(messageManagement));
    }

    /**
     * 通过id删除消息管理
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除消息管理", notes = "通过id删除消息管理")
    @SysLog("通过id删除消息管理")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('message_messagemanagement_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(messageManagementService.removeById(id));
    }


    /**
     * 导出excel 表格
     *
     * @param messageManagement 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('message_messagemanagement_export')")
    public List<MessageManagement> export(MessageManagement messageManagement) {
        return messageManagementService.list(Wrappers.query(messageManagement));
    }

    /**
     * 将消息信息对树结构返回
     *
     * @return 树集合结构
     */
    @GetMapping("/tree")
    public R<List<Tree<Long>>> selectTree() {
        return R.ok(messageManagementService.selectTree());
    }

    @Inner
    @PostMapping("/listManagementUserObjectMode")
    public R<List<ManagementUserObjectVO>> listManagementUserObjectMode(@RequestBody ManagementUserObjectSearchDTO searchDTO) {
        return R.ok(messageManagementService.listManagementUserObjectMode(searchDTO));
    }

    @Inner
    @PostMapping("/listMessageRules/{id}")
    public R<List<MessageRuleVO>> listMessageRules(@PathVariable("id") Long id) {
        return R.ok(messageManagementService.listMsgRule(id));
    }


    @Inner
    @PostMapping("/listManagement")
    public R<List<ManagementUserObjectVO>> listManagement(@RequestBody ManagementUserObjectSearchDTO searchDTO) {
        return R.ok(messageManagementService.listManagement(searchDTO));
    }
}
